package com.fairytask.ui
{
	import com.fairytask.data.Modulator;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.components.text.Text;
	import com.fairytask.engine.components.window.Window;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.managers.TipsManage;
	import com.fairytask.net.CustomToClientEvent;
	import com.fairytask.ui.base.FrameUI;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;

	/**
	 * 邮件窗口界面
	 * @author pangj
	 * 
	 */
	public class MailFrameUI extends FrameUI
	{
		private static var _self :MailFrameUI=null;
		
		//界面控件
		public var preBtn        :Button;    //上一页 按钮
		public var pageLabel     :Label;           //页签
		public var nextBtn       :Button;    //下一页 按钮
		public var deleteAll     :Button;          //删除所有 按钮
		public var deleteMail    :Button;          //删除邮件 按钮
		public var getAppendix   :Button;          //提取附件按钮
		public var holderMails   :Window;          //显示邮件列表
		public var title         :Label;	        //标题标签

		public var theme         :Label;           //主题标签		
		public var showTheme     :Window;          //主题背景
		public var themeLabel    :Label;           //主题内容
	
		public var content       :Label;            //内容标签
		public var contentWin    :Window            //内容窗口
		public var contentText   :Text;             //内容文本
		
		public var appendix      :Label;            //附件标签
		public var appWin1       :MovieClip;        //附件1窗口
		public var appWin2       :MovieClip;        //附件2窗口
		public var appWin3       :MovieClip;        //附件3窗口
		private var appsImg      :Array;            //附件图标
		private var appsNum      :Array;            //单个附件中物品数量
	
		//----------
		private var _model       :Modulator = Modulator.instance;	
		public var pageSize      :int               //每页显示邮件数
		private var thePage      :int;              //当前页码
		private var maxPage      :int;              //总页码
		private var count        :int;              //所有邮件总数
			
		private var mailsInfo    :Object;           //接收到的邮件列表信息
		private var showMails    :Array;            //当前显示的邮件列表
		private var choose       :int;              //当前选中的邮件ID
		
		
		
		public static function getInstance():MailFrameUI
		{
			if(_self==null)
			{
				_self=new MailFrameUI();
			}
			return _self;
		}
		public function MailFrameUI(popUI:Boolean=true)
		{
			super(popUI);
			
			thePage = 1;
			initEvent();
			getAppendix.visible = false;
			deleteMail.visible = true;			
		}
	
		/**
		 * 事件初始化 
		 * 
		 */
		public function initEvent():void
		{
			//接收邮件列表
			//_model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_SEND_MAIL,getMails);
			//接收单个邮件信息
			//_model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_SEND_LONE_MAIL,showDetails);
			
			preBtn.addEventListener(MouseEvent.CLICK,onPageUp);
			nextBtn.addEventListener(MouseEvent.CLICK,onPageDown);
			
			deleteMail.addEventListener(MouseEvent.CLICK,onDelete);
			deleteAll.addEventListener(MouseEvent.CLICK,onDeleteAll);
			getAppendix.addEventListener(MouseEvent.CLICK,onGetApp);
					
		}
		/**
		 * 显示邮件列表 
		 * 
		 */
		private function onShowList():void
		{
			//显示页签
			maxPage = count%pageSize!=0?int(count/pageSize)+1:int(count/pageSize);
			if(maxPage<1)
				maxPage=1;
			if(thePage>maxPage)
				thePage=maxPage;
			if(thePage<1)
				thePage=1;
			pageLabel.htmlStr = thePage+"/"+maxPage;
			
			//显示邮件
			showMails = new Array();
			for(var i:int=0; i<pageSize && i<mailsInfo.length; i++)
			{
				var onMail :MailInfoUI = new MailInfoUI(mailsInfo[i].Key);
				onMail.x=5;
				onMail.y=5+i*onMail.height;

				onMail.MailImg    = mailsInfo[i].IsRead;
				onMail.MailTitle  = mailsInfo[i].Subject;
				onMail.GetTime    = mailsInfo[i].Date;
				
				showMails[i] = onMail;
				holderMails.addChild(showMails[i]);
			}
		
		}
		
		/**
		 * 点击上一页,显示上一页邮件
		 * 
		 */
		private function onPageUp(e:MouseEvent):void
		{
			if(thePage>1)
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTMMSG_CLIENT_MAILBOX,--thePage);
			}

		}
		/**
		 * 点击下一页，显示下一页邮件
		 * 
		 */
		private function onPageDown(e:MouseEvent):void
		{
			if(thePage<maxPage)
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTMMSG_CLIENT_MAILBOX,++thePage);
				
			}
		}
		
		/**
		 *选择邮件，阅读 或 删除 
		 * @param e        点击事件
		 * @param mailID   邮件key值
		 * @param isDelete 是否删除  (true:删除   false:阅读)
		 * 
		 */
		public function sendDetails(e:MouseEvent,mailID:int,isDelete:Boolean):void
		{
			//设置选中邮件外框加亮
			if(null!=showMails[choose%pageSize])
			{
				showMails[(count-1-choose)%pageSize].windowBG.filters = null;
			}
			choose = mailID;
			if(null!=showMails[choose%pageSize])
			{
				showMails[(count-1-choose)%pageSize].windowBG.filters = [Filter.YELLOW_GLOW];
			}
			//判断是否删除选中的邮件 true：删除   false：读取
			if(isDelete)
			{
				onDelete(e);
			}
			else
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTMMSG_READ_MAIL,mailID);
			}
		}
		/**
		 * 显示邮件内容 
		 * 
		 */
		private function showDetails(evt:CustomToClientEvent):void
		{
			removeElement(appsImg);
			removeElement(appsNum);
			appsImg = [];
			appsNum = [];
		
			/*themeLabel.htmlStr  = evt.pList.shift(); //主题
			contentText.htmlStr = evt.pList.shift(); //内容
			evt.pList.shift();                       //发送日期
			var length : int = evt.pList.shift();   //附件数量 
			deleteMail.visible  = length == 0;
			getAppendix.visible = length != 0;
			for(var i:int=1;i<=length;i++)
			{
				createApp(evt.pList.shift(),evt.pList.shift(),i);
			}*/
		}
		/**
		 *创建附件显示
		 * @param id    附件物品configID
		 * @numOfApp    附件数量
		 * @param index 附件物品索引值  第index个附件
		 * 
		 */
		private function createApp(configID:int,numOfApp:String,index:int):void
		{
			var app:Clip = new Clip();
			app.source = ResConfig.iconPath+configID+ResConfig.iconImg;
			app.x = 2;
			app.y = 2;
			app.addEventListener(MouseEvent.ROLL_OVER,function(mevt:MouseEvent):void
			{
				TipsManage.ShowItemTips(configID);
			});
			app.addEventListener(MouseEvent.ROLL_OUT,function(mevt:MouseEvent):void
			{
				ToolTips.hide();
			});
			var num:Label = new Label();
			num.htmlStrByBorder = numOfApp;
			num.x = 50-num.width;
			num.y = 30;
			appsImg[index-1]=app;
			appsNum[index-1]=num;
			getAppBG(index).addChild(appsImg[index-1]);
			getAppBG(index).addChild(appsNum[index-1]);
		}
		/**
		 *根据附件排序获取附件的背景窗口 
		 * @param index 附件索引值  第index个附件
		 * @return 对应附件的窗口
		 * 
		 */
		private function getAppBG(index:int):MovieClip
		{
			return index==1?appWin1:(index==2?appWin2:appWin3);
		}
	
		/**
		 * 获取邮件列表信息
		 * 
		 */
		public function getMails(evt:CustomToClientEvent):void
		{
			choose = 0;
			//获取邮件总数
			/*count = evt.pList[1];
			//获取该页邮件信息
			mailsInfo = com.adobe.serialization.json.JSON.decode(evt.pList[2]);*/
			
			removeElement(showMails);
			removeElement(appsNum);
			removeElement(appsImg);
			onShowList();
			getAppendix.visible = false;
			deleteMail.visible = true;
		}
		/**
		 * 移除前页显示组件
		 * 
		 */
		private function removeElement(child:Array):void
		{
			themeLabel.htmlStr = "";
			contentText.htmlStr     = "";
			if(null!=child)
				for(var i:int=0;i<child.length;i++)
				{
					if(null!=child[i].parent)
					{
						child[i].parent.removeChild(child[i]);
					}
				}
		}
		/**
		 *删除邮件
		 * 
		 */
		public function onDelete(evt:MouseEvent):void
		{
			if(choose>=0)
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTMMSG_DESTORY_MAIL,2,thePage,choose);
				removeElement(appsImg);
			}
		}
		/**
		 * 删除所有邮件
		 * 
		 */
		private function onDeleteAll(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTMMSG_DESTORY_MAIL,1);
			removeElement(appsImg);
		}
		/**
		 * 提取附件
		 * 
		 */
		private function onGetApp(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTMMSG_GET_ANNEX,choose);
		}
	}
}